package com.yt.lxh.product.controller;

import com.github.pagehelper.PageInfo;
import com.yt.lxh.common.result.ResponseResult;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.vo.product.TasteListVO;
import com.yt.lxh.dto.product.SaveTasteDTO;
import com.yt.lxh.dto.product.UpdateTasteDTO;
import com.yt.lxh.product.service.TasteService;
import com.yt.lxh.dto.common.BasePageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品口味管理接口
 *
 * @author yzk789
 * @since 2023-10-18
 */
@Api(tags = "商品口味管理接口")
@RestController
@RequestMapping("/admin/product/taste")
@CrossOrigin
public class TasteController {

    @Autowired
    private TasteService tasteService;

    @ApiOperation("添加口味")
    @PostMapping("/save")
    public ResponseResult addTaste(
            @Validated @RequestBody SaveTasteDTO saveTasteDTO,
            @RequestHeader(value = "token", required = false) String token) {
        tasteService.saveTaste(saveTasteDTO, token);
        return ResponseResult.ok(ResultCodeEnum.INSERT_SUCCESS);
    }


    @ApiOperation("获取全部口味数据")
    @GetMapping("/list")
    public ResponseResult getTasteAll() {
        List<TasteListVO> tasteAll = tasteService.getTasteAll();
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, tasteAll);
    }

    @ApiOperation("获取全部口味数据（分页）")
    @PostMapping("/page")
    public ResponseResult getTastePage(@RequestBody BasePageDTO basePageDTO) {
        PageInfo<TasteListVO> tasteList = tasteService.getTastePage(basePageDTO);
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, tasteList);
    }

    @ApiOperation("更新口味")
    @PutMapping("/update")
    public ResponseResult updateTaste(
            @Validated @RequestBody UpdateTasteDTO updateTasteDTO,
            @RequestHeader(value = "token", required = false) String token) {
        tasteService.updateTaste(updateTasteDTO, token);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("根据ID逻辑删除口味")
    @DeleteMapping("/remove/{id}")
    public ResponseResult delTaste(
            @PathVariable String id,
            @RequestHeader(value = "token", required = false) String token) {
        tasteService.delTasteById(id, token);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }

    @ApiOperation("根据ID列表批量删除口味")
    @DeleteMapping("/remove/batch")
    public ResponseResult delTasteByBatch(
            @RequestBody List<String> idList,
            @RequestHeader(value = "token", required = false) String token) {
        tasteService.delTasteByBatchIds(idList, token);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }

}

